package com.xp.tinyurl.admin.controller;

import com.xp.tinyurl.admin.vo.LinkStatisticVO;
import com.xp.tinyurl.common.base.ResultVO;
import com.xp.tinyurl.common.base.ResultVOBuilder;
import com.xp.tinyurl.model.Statistics;
import com.xp.tinyurl.service.StatisticsService;
import com.xp.tinyurl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * @Description 统计（暂缓接口）
 * @Author chengguo.pei
 * @Date 2021/9/22 18:15
 */
@Slf4j
@RequestMapping("/admin/statistics")
@RestController
public class StatisticsDataController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 查询短链接从当日零时至今调用次数
     * @param appKey
     * @return
     */
    @RequestMapping(value = "/getOneDayStatistics")
    public ResultVO getOneDayStatisticsData(String appKey) {
        long endTime = System.currentTimeMillis();
        Map<String, Object> param = new HashMap<>(3);
        param.put("startTime", new Date(endTime - (endTime + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24)));
        param.put("endTime", new Date(endTime));
        param.put("appKey", appKey);

        List<Statistics> statisticsList = statisticsService.getMinuteStatisticsByAppKey(param);
        List<LinkStatisticVO> linkStatisticVOList = new ArrayList<>();
        for (Statistics statistics : statisticsList){
            LinkStatisticVO linkStatisticVO = new LinkStatisticVO();
            linkStatisticVO.setSucceed(statistics.getSucceed());
            linkStatisticVO.setTotal(statistics.getTotal());
            linkStatisticVO.setTime(DateUtils.getSdf(DateUtils.HH_MM).format(statistics.getStatisticsTime()));
            linkStatisticVOList.add(linkStatisticVO);
        }
        return ResultVOBuilder.success(linkStatisticVOList);
    }


}
